<?php

namespace App\Admin\Controllers;

use App\Models\ShopRoomTable;
use App\Models\Shop;
use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class RoomTableController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('房桌信息');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('房桌修改');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('房桌创建');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ShopRoomTable::class, function (Grid $grid) {
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('shop.shop_name', '店铺名');
                $filter->like('name', '房桌名');
            });
            $grid->disableCreateButton();//禁用新增
            $grid->id('ID')->sortable();
            if(request('shop_id')!=''){
                $grid->model()->where('shop_id',request('shop_id'));
            }

            $grid->column('shop.shop_name','店铺名');
            $grid->type('房桌')->display(function ($type){
                if($type == 0){
                    return '桌子';
                }else{
                    return '房间';
                }
            });
            $grid->column('hold','容纳人数');
            $grid->column('name','房桌名');
            $grid->is_use('是否使用中')->display(function ($is_use){
                if($is_use == 0){
                    return '否';
                }else{
                    return '是';
                }
            });
            $grid->lock_qrcode('是否启用二维码')->display(function ($lock_qrcode){
                if($lock_qrcode == 0){
                    return '否';
                }else{
                    return '是';
                }
            });
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ShopRoomTable::class, function (Form $form) {
            $form->saving(function (Form $form) {
                //店员处理
                if ($form->shoper_id) {
                    $shoper_id = '';
                    for ($x = 0; $x < count($form->shoper_id) - 1; $x++) {
                        $shoper_id .= $form->shoper_id[$x] . ',';
                    }
                    $shoper_id = rtrim($shoper_id, ",");
                }

                //修改
                if(request()->route()->parameters()){
                    $data = ShopRoomTable::where('id',request()->route()->parameters())->update([
                        'is_use' => $form->is_use,
                        'shoper_id' => $shoper_id,
                        'type' => $form->type,
                        'hold' => $form->hold,
                        'name' => $form->name,
                        'min_consumption' => $form->min_consumption,
                        'note' => $form->note,
                        'lock_qrcode' => $form->lock_qrcode
                    ]);
                    if($data>0) {
                        echo "<script> alert('保存成功！');history.go(-2); </script>";
                        die;
                    }else{
                        echo "<script> alert('保存失败，请重试！');history.back(); </script>";
                        die;
                    }
                }

                //新增
                $user_id = Shop::where('id', request('shop_id'))->first()->user_id;
                //图片处理
                if ($form->thumb) {
                    $thumb = $form->thumb;
                    $thumb_data = '';
                    for ($i = 0; $i < count($thumb); $i++) {
                        $name = md5($_FILES['thumb']['name'][$i]);
                        $filename = "/storage/user_$user_id/info/$name.jpg";
                        move_uploaded_file($thumb[$i], ".$filename");
                        $thumb_data .= $filename . ';';
                    }
                    $thumb_data = rtrim($thumb_data, ";");
                } else {
                    $thumb_data = '';
                }

                $create = ShopRoomTable::create([
                    'shop_id' => request('shop_id'),
                    'shoper_id' => $shoper_id,
                    'type' => $form->type,
                    'hold' => $form->hold,
                    'name' => $form->name,
                    'min_consumption' => $form->min_consumption,
                    'lock_qrcode' => $form->lock_qrcode,
                    'note' => $form->note,
                    'thumb' => $thumb_data,
                ]);
                if (!$create) {
                    echo "<script> alert('保存失败，请重试！');history.back(); </script>";
                    die;
                }
                echo "<script> alert('保存成功！');history.back(); </script>";
                die;
            });


            $form->text('shop_id', '店铺id')->value(request('shop_id'));
            if(request()->route()->parameters()) {
                $form->select('is_use', '是否使用中')->options([0 => '否', 1 => '是']);
            }
            $data = User::all();
            $res = array();
            foreach ($data as $key => $value) {
                $res[$value['id']] = $value['mobile'];
            }
            $form->listbox('shoper_id', '店员id')->options($res);
            // $form->text('shoper_id','店员id');
            $form->select('type', '房桌')->options([0 => '桌子', 1 => '房间']);
            $form->text('hold', '容纳人数')->rules('required');
            $form->text('name', '名字')->rules('required');
            $form->text('min_consumption', '最低消费');
            $form->text('note', '说明');
            $form->select('lock_qrcode', '是否启用二维码')->options([0 => '否', 1 => '是']);
            if(request('shop_id')) {
                $form->multipleImage('thumb', '图片')/*->move("user_$user_id/info")->uniqueName()*/;
                $form->multipleImage('thumb', '图片')/*->move("user_$user_id/info")->uniqueName()->rules('required')*/;
                $form->multipleImage('thumb', '图片')/*->move("user_$user_id/info")->uniqueName()->rules('required')*/;
            }
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
